﻿
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using WWLauncher.Constant;
using WWLauncher.ViewModel;

namespace WWLauncher.Util
{
    /// <summary>
    /// 提取一些代码
    /// </summary>
    class CommonCode
    {


        /// <summary>
        /// 根据路径获取文件图标等信息
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static IconViewModel GetIconInfoByPath(string path)
        {
            string tempPath = path;

            string targetPath = FileUtil.GetTargetPathByLnk(path);
            var iconPath = FileUtil.GetIconPathByLnk(path);
            if (targetPath != null)
            {
                path = targetPath;
            }
            if (iconPath.IsNullOrEmpty())
            {
                iconPath = path;
            }

            BitmapImage bi = ImageUtil.GetBitmapIconByPath(iconPath);
            var iconInfo = new IconViewModel
            {
                Id = Guid.NewGuid(),
                TargetPath = path,
                LnkPath = tempPath,
                BitmapImage = bi,
                StartArg = FileUtil.GetArgByLnk(tempPath)
            };
            iconInfo.DefaultImageArr = iconInfo.ImageByteArr;
            iconInfo.Name = System.IO.Path.GetFileNameWithoutExtension(tempPath);
            if (iconInfo.Name.IsNullOrEmpty())
            {
                iconInfo.Name = path;
            }
            string relativePath = FileUtil.MakeRelativePath(Constants.AppExe, iconInfo.TargetPath);
            if (!string.IsNullOrEmpty(relativePath) && !relativePath.Equals(iconInfo.TargetPath))
            {
                iconInfo.RelativePath = relativePath;
            }
            return iconInfo;
        }


        public static IconViewModel GetIconInfoByPath_NoWrite(string path)
        {
            string tempPath = path;

            //string base64 = ImageUtil.FileImageToBase64(path, System.Drawing.Imaging.ImageFormat.Png);
            string ext = "";
            if (!ImageUtil.IsSystemItem(path))
            {
                ext = System.IO.Path.GetExtension(path).ToLower();
            }

            string iconPath = null;
            if (".lnk".Equals(ext))
            {

                string targetPath = FileUtil.GetTargetPathByLnk(path);
                iconPath = FileUtil.GetIconPathByLnk(path);
                if (targetPath != null)
                {
                    path = targetPath;
                }
            }
            if (iconPath.IsNullOrEmpty())
            {
                iconPath = path;
            }

            BitmapImage bi = ImageUtil.GetBitmapIconByPath(iconPath);
            var iconInfo = new IconViewModel
            {
                TargetPath = path,
                LnkPath = tempPath,
                BitmapImage = bi,
                StartArg = FileUtil.GetArgByLnk(tempPath)
            };
            iconInfo.DefaultImageArr = iconInfo.ImageByteArr;
            iconInfo.Name = Path.GetFileNameWithoutExtension(tempPath);
            if (iconInfo.Name.IsNullOrEmpty())
            {
                iconInfo.Name = path;
            }
            return iconInfo;
        }

        /// <summary>
        /// 排序图标
        /// </summary>
        public static void SortIconList()
        {
            try
            {
                var mainViewModel = MainViewModel.Instance();
                if (mainViewModel.AppConfig.IconSortType != SortType.Custom)
                {
                    var menuList = mainViewModel.MenuList;
                    foreach (MenuViewModel menuInfo in menuList)
                    {
                        var list = new List<IconInfo>(menuInfo.IconList);
                        switch (mainViewModel.AppConfig.IconSortType)
                        {
                            case SortType.CountUp:
                                list.Sort((x, y) => x.Count.CompareTo(y.Count));
                                break;
                            case SortType.CountLow:
                                list.Sort((x, y) => y.Count.CompareTo(x.Count));
                                break;
                            case SortType.NameUp:
                                list.Sort((x, y) => string.Compare(x.Name, y.Name, StringComparison.Ordinal));
                                break;
                            case SortType.NameLow:
                                list.Sort((x, y) => string.Compare(y.Name, x.Name, StringComparison.Ordinal));
                                break;
                        }
                        menuInfo.IconList = new ObservableCollection<IconInfo>(list);
                    }
                    mainViewModel.SelectedMenuIcons = mainViewModel.MenuList[mainViewModel.SelectedMenuIndex].IconList;
                }
            }
            catch (Exception)
            {
                // ignored
            }
        }




    }
}
